from cefpython3 import cefpython as cef
import tkinter as tk
import sys

url = input("please input url:")

class ChromiumBrowser:
    def __init__(self):

        # CEF设置
        self.settings = {
            "windowless_rendering_enabled": False,
            "multi_threaded_message_loop": False,
            "cache_path": "cache",  # 浏览器缓存目录
            "persist_session_cookies": True  # 保持会话cookies
        }

        # 初始化CEF
        cef.Initialize(settings=self.settings)

        # 创建浏览器窗口
        self.create_browser()

    def create_browser(self):
        """创建浏览器窗口"""
        window_info = cef.WindowInfo()
        window_info.SetAsChild(0)  # 0表示创建顶级窗口

        # 浏览器配置
        browser_settings = {
            "file_access_from_file_urls_allowed": True,
            "universal_access_from_file_urls_allowed": True,
            "web_security_disabled": False
        }

        # 创建浏览器实例
        self.browser = cef.CreateBrowserSync(
            window_info,
            url=url,
            settings=browser_settings
        )

        # 设置窗口标题
        # self.browser.SetTitle("CEFPython3 独立浏览器")

    def run(self):
        """运行消息循环"""
        cef.MessageLoop()

    def shutdown(self):
        """关闭浏览器"""
        self.browser.CloseBrowser(True)
        cef.Shutdown()


if __name__ == "__main__":
    browser = ChromiumBrowser()
    try:
        browser.run()
    except KeyboardInterrupt:
        browser.shutdown()
